﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgLab.CalendarSyncUtil
{
    public class CalendarPair
    {
        protected ICalendarItem calendarA = null;
        protected ICalendarItem calendarB = null;

        public bool IsNeedSync
        {
            get
            {
                if (calendarA != null && calendarB != null)
                    return calendarA.IsSameEvent(calendarB);
                else
                    return false;
            }
        }

        public CalendarPair(ICalendarItem calendarA, ICalendarItem calendarB)
        {
            this.calendarA = calendarA;
            this.calendarB = calendarB; 
        }
    }

    public class CalendarSyncHelper
    {
        public List<CalendarItem> Source = null;
        public List<CalendarItem> Destination = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="calendar">要被加入議程的行事曆</param>
        /// <returns>成功加入的議程數</returns>
        public int Sync(ICalendarBook calendar)
        {
            int totalSync = 0;
            foreach (CalendarItem srcItem in Source)
            {
                bool isSame = false;
                foreach (CalendarItem dstItem in Destination)
                {
                    if (srcItem.IsSameEvent(dstItem))
                    {
                        isSame = true;
                        break;
                    }
                }

                if (!isSame)
                {
                    if (calendar.AddAppointment(srcItem))
                    {
                        Console.WriteLine("Sync : ");
                        Console.WriteLine(srcItem.Subject + "|" + srcItem.Content + "|" + srcItem.Location + "|" + srcItem.Start.ToString() + "|" + srcItem.End.ToString());
                        totalSync++;
                    }
                }
            }

            return totalSync;
        }
    }
}
